package com.zhiyou100.controller;

import java.util.HashMap; 
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.ChargeManager;
import com.zhiyou100.service.ItemsService;

@Controller
public class Pay_ItemsController {
	@Autowired	
	private ItemsService itemsService;
	@RequestMapping("/items/list")
	public String itemsList(Model model,
			@RequestParam(defaultValue="")String medicalRecord,
			@RequestParam(defaultValue="")String patientName,
			@RequestParam(defaultValue="1")int pageNo) {
		Map<String,Object> map = new HashMap<>();
		map.put("medicalRecord", medicalRecord);
		map.put("patientName", patientName);
		PageHelper.startPage(pageNo, 2);
		// 调用服务层
		List<ChargeManager> charge = itemsService.findAll(map);
		System.out.println("从数据库获取的: "+charge);
		// 分页
		System.out.println("pageNo: "+pageNo);
		PageInfo<ChargeManager> pageInfo = new PageInfo<>(charge);
		model.addAttribute("map",map);
//		model.addAttribute("charge", charge);
		model.addAttribute("charge",pageInfo.getList());
		model.addAttribute("page",pageInfo);
		System.out.println("pageInfo: "+pageInfo);
		return "forward:/view/hospital/chargeList.jsp";
	}
	// 添加
	@RequestMapping(value="/items/add",method=RequestMethod.GET)
	public String addCharge(int medical_record,Model model) {
		ChargeManager charge = itemsService.findToAdd(medical_record);
		model.addAttribute("charge", charge);
		return "forward:/view/hospital/chargeAdd.jsp";
	}
	@RequestMapping(value="/items/add",method=RequestMethod.POST)
	public String addcharge(ChargeManager charge) {
		System.out.println("添加时从页面接受到的charge: "+charge);
		itemsService.addCharge(charge);
		return "forward:/items/list";
	}
	
	// 不通过参数添加
	@RequestMapping(value="/items/add2",method=RequestMethod.GET)
	public String addcharge2() {
		return "forward:/view/hospital/chargeAdd2.jsp";
	}
	@RequestMapping(value="/items/add2",method=RequestMethod.POST)
	public String addcharge2(ChargeManager charge) {
		itemsService.addCharge2(charge);
		return "forward:/items/list";
	}
}
